﻿using System;
using SharpSvn;

namespace SvnFlow
{
    public class BranchInfo
    {
        public Uri Uri { get; private set; }
        public string Name { get; private set; }
        public string FullPath { get; private set; }
        public long PrevRevision { get; private set; }
        public long LastRevision { get; private set; }

        public long? WorkingCopyRevision { get; set; }


        public SvnRevisionRange Range
        {
            get
            {
                if (WorkingCopyRevision == null)
                    throw new InvalidOperationException("WorkingCopyRevision not set.");

                return new SvnRevisionRange(LastRevision, WorkingCopyRevision.Value);
            }
        }

        public BranchInfo(Uri uri, string name, string fullPath, SvnRevisionRange revisions)
        {
            Uri = uri;
            Name = name;
            FullPath = fullPath;
            PrevRevision = revisions.StartRevision.Revision;
            LastRevision = revisions.EndRevision.Revision;

            WorkingCopyRevision = null;
        }

        public void UpdateRevisions()
        {
            if (WorkingCopyRevision == null)
                throw new InvalidOperationException("WorkingCopyRevision not set.");

            PrevRevision = LastRevision;
            LastRevision = WorkingCopyRevision.Value;
        }

        public bool Equals(BranchInfo other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;

            return
                other.Uri == Uri
                && other.Name == Name
                && other.FullPath == FullPath
                && other.PrevRevision == PrevRevision
                && other.LastRevision == LastRevision
                && other.WorkingCopyRevision == WorkingCopyRevision;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;

            var obj1 = obj as BranchInfo;
            if (ReferenceEquals(null, obj1)) return false;

            return Equals(obj1);
        }

        public override int GetHashCode()
        {
            return (Uri + "|" + Name + "|" + FullPath + "|" + PrevRevision + "|" + LastRevision
                    + "|" + WorkingCopyRevision).GetHashCode();
        }
    }
}
